/**
 * Created by ${PRODUCT_NAME}.
 * User: ddascal
 * Date: 12/21/10
 * Time: 8:07 AM
 * To change this template use File | Settings | File Templates.
 */
package org.flexthinker.binding.bean
{
    public class Binding
    {
        public var _isBound:Boolean = false;
        public var _isManaged:Boolean = false;

        private var sourceObject:Object;
        private var sourceProperty:PropertyHelper;
        private var targetObject:Object;
        private var targetProperty:PropertyHelper;

        private var name:String;

        /**
         * Create an instance of {@code Binding} between two properties of two
         * objects.
         *
         * @param sourceObject the source object
         * @param sourceProperty a property on the source object
         * @param targetObject the target object
         * @param targetProperty a property on the target object
         * @param name a name for the {@code Binding}
         * @throws IllegalArgumentException if the source property or target property
         *           is {@code null}
         */
        public function Binding(sourceObject:Object, sourceProperty:PropertyHelper,
                                targetObject:Object, targetProperty:PropertyHelper,
                                name:String):void
        {
            this.sourceObject = sourceObject;
            this.sourceProperty = sourceProperty;
            this.targetObject = targetObject;
            this.targetProperty = targetProperty;
            this.name = name;
        }

        public function getName():String
        {
            return name;
        }


        public function get isBound():Boolean
        {
            return _isBound;
        }

        public function get isManaged():Boolean
        {
            return _isManaged;
        }

        public function bind():void
        {
            throwIfManaged();
            bindUnmanaged();
        }

        // -------------------
        //   internal methods
        // -------------------

        protected function throwIfManaged():void
        {
            if ( _isManaged )
            {
                throw new Error("Binding is already managed.")
            }
        }

        protected function throwIfBound():void
        {
           if ( _isBound )
           {
               throw new Error("Binding is already bound");
           }
        }

        protected function bindUnmanaged():void
        {
            throwIfBound();

        }




    }
}
